<template>
   <div class="page-box">
      <div class="page-title">
         {{ "供应商管理" }}
      </div>

      <div class="page-search">
         <div
            class="search-box"
            :style="{ height: IsHeight }"
         >
            <el-input
               v-model="pageData.keywords"
               placeholder="供应商名称/供应商级别/联系人"
               style="width: 240px; margin-right: 10px; margin-bottom: 10px"
               suffix-icon="search"
               @input="handleInput"
            />
         </div>
         <div class="search-btn">
            <div
               class="btn_item"
               @click="handleClickHeight"
            >
               <img
                  class="btn_img"
                  src="@/assets/sx.png"
                  alt=""
               />
               <span> 高级筛选 </span>
            </div>
            <div
               class="btn_item"
               @click="handleExport"
            >
               <img
                  class="btn_img"
                  src="@/assets/dc.png"
                  alt=""
               />
               <span> 导出 </span>
            </div>
            <div
               class="btn_item"
               @click="
                  () => {
                     ElMessage.info('暂未开发');
                  }
               "
            >
               <img
                  class="btn_img"
                  src="@/assets/dr.png"
                  alt=""
               />
               <span> 导入 </span>
            </div>
         </div>
      </div>
      <div
         class="search-btn-right"
         style="right: 9px"
      >
         <el-button
            color="#626aef"
            type="primary"
            @click="handleAddSupplier"
         >
            新增供应商
         </el-button>
      </div>
   </div>

   <div class="table-box">
      <el-table
         :data="tabeData"
         style="width: 100%"
         v-loading="loading"
         border
      >
         <el-table-column
            align="center"
            prop="name"
            label="供应商名称"
            width="100"
         />
         <el-table-column
            align="center"
            prop="supplier_level_name"
            label="供应商级别"
            width="240"
         />
         <el-table-column
            align="center"
            prop="contact_person"
            label="联系人"
            width="240"
         >
         </el-table-column>

         <el-table-column
            align="center"
            prop="contact_phone"
            label="联系人电话"
            width="240"
         >
         </el-table-column>

         <el-table-column
            align="center"
            prop="contact_email"
            label="联系人邮箱"
            width="230"
         />
         <el-table-column
            align="center"
            prop="contact_position"
            label="联系人职位"
            width="140"
         />
         <el-table-column
            align="center"
            prop="city"
            label="地区"
            width="140"
         />
         <el-table-column
            align="center"
            prop="address"
            label="详细地址"
            width="140"
         />
         <el-table-column
            align="center"
            prop="note"
            label="备注"
            width="140"
         />
         <el-table-column
            align="center"
            prop="creator_name"
            label="创建人"
            width="140"
         />
         <el-table-column
            align="center"
            prop="created_at"
            label="创建时间"
            width="140"
         />
         <el-table-column
            fixed="right"
            label="操作"
            min-width="130"
            prop="id"
            align="center"
         >
            <template #default="scope">
               <div class="btn-box">
                  <el-button
                     plain
                     size="small"
                     type="primary"
                     @click="handleEditSupplier(scope.row.id)"
                  >
                     编辑
                  </el-button>
                  <el-button
                     plain
                     size="small"
                     type="danger"
                     @click="handleDeleteSupplier(scope.row.id)"
                  >
                     删除
                  </el-button>
               </div>
            </template>
         </el-table-column>
      </el-table>
   </div>

   <!-- 分页 -->
   <Pagination
      class="pagination"
      @pageSize="pageSize"
      @getPage="getPage"
      :page="pageData.page"
      :page-size="pageData.pageSize"
      :total="total"
   />

   <Dialog
      ref="dialogRef"
      @refresh="getAlltabeData"
   />
</template>

<script setup lang="ts">
   import { ref, onMounted, watch } from "vue";
   import { useRouter } from "vue-router";
   import { usePlatformManagementStore } from "@/stores";
   import Pagination from "@/components/Pagination.vue";
   import { throttle } from "lodash-es";
   import { SupplierList } from "./suppliersType.ts";
   import { exportTableToExcel, formatters } from "@/utils/exportTable";
   const platformManagementStore = usePlatformManagementStore();
   const router = useRouter();
   import api from "@/api/index.js";
   import Dialog from "./components/AddSupplier.vue";
   import { ElMessage, ElMessageBox } from "element-plus";

   let IsHeight = ref<number | string>(0);

   const search = ref<string>("");
   const dialogRef = ref<any>(null);
   const pageData = ref({
      pageSize: 10,
      page: 1,
      supplier_level_id: "", // 供应商级别
      keywords: "", // 搜索关键字
   });
   const total = ref(0);
   const tabeData = ref<SupplierList[]>([]);
   // 侦听动态分页
   watch(
      () => {
         return pageData.value;
      },
      () => {
         getAlltabeData();
      },
      { deep: true }
   );
   // 新增供应商
   const handleAddSupplier = () => {
      dialogRef.value.openLoading();
   };
   let nub = ref<number>(0);
   const handleClickHeight = () => {
      nub.value++;
      if (nub.value % 2 == 1) {
         IsHeight.value = "40px";
      } else {
         IsHeight.value = "0px";
      }
   };
   // 编辑供应商
   const handleEditSupplier = (id: number) => {
      dialogRef.value.openLoading(id);
   };
   // 删除供应商
   const handleDeleteSupplier = (id: number) => {
      ElMessageBox.confirm("确定删除该供应商吗？", "提示", {
         confirmButtonText: "确定",
         cancelButtonText: "取消",
         type: "warning",
      }).then(() => {
         api.supplier
            .supplierDelete({
               id: id,
            })
            .then((res) => {
               if (res.code == 200) {
                  ElMessage.success("删除成功");
                  getAlltabeData();
               } else {
                  ElMessage.error(res.message);
               }
            })
            .catch((err) => {
               console.log(err);
            });
      });
   };
   const handleExport = () => {
      exportTableToExcel({
         data: tabeData.value,
         fileName: "供应商管理", //文件名
         sheetName: "供应商管理", //工作表名
         //导出字段配置
         fields: [
            { field: "name", title: "供应商名称" },
            {
               field: "supplier_level_name",
               title: "供应商级别",
               formatter: (value) => (value === 1 ? "一级" : "二级"),
            },
            { field: "contact_person", title: "联系人" },
            { field: "contact_phone", title: "联系电话" },
            { field: "contact_email", title: "联系邮箱" },
            { field: "contact_position", title: "联系人职位" },
            { field: "city", title: "地区" },
            { field: "address", title: "详细地址" },
            { field: "note", title: "备注" },
            { field: "creator_name", title: "创建人" },
            {
               field: "created_at",
               title: "创建时间",
               formatter: formatters.formatDate,
            },
         ],
      });
   };
   const pageSize = (e: number) => {
      pageData.value.pageSize = e;
   };
   const getPage = (e: number) => {
      pageData.value.page = e;
   };
   onMounted(() => {
      getAlltabeData();
   });
   // 获取门店列表
   const getAlltabeData = async () => {
      const res = await api.supplier.supplierList({
         ...pageData.value,
      });
      if (res.code == 200) {
         tabeData.value = res.data.data;

         total.value = res.data.pager.page_count;
      }
   };
   // 处理输入
   const handleInput = throttle(() => {
      getAlltabeData();
   }, 1000); // 500ms 内仅执行一次

   const loading = ref(false);
</script>

<style lang="scss" scoped>
   .btn-box {
      display: flex;
      align-items: center;
      justify-content: center;
   }

   // 输入框样式
   ::v-deep .el-input__wrapper {
      background-color: rgba(250, 252, 251, 1);
   }

   //表格样式

   ::v-deep .el-table {
      --el-table-header-bg-color: rgba(235, 236, 240, 1);
   }

   ::v-deep .el-table thead {
      color: #444;
   }

   //分页样式
   .pagination {
      width: calc(100% - 250px);
      position: fixed;
      bottom: 20px;
      right: 20px;
      padding-top: 10px;
      border-top: 1px solid #98989848;
   }

   ::v-deep .el-pagination.is-background .el-pager li.is-active {
      background-color: #000 !important;
   }
   :deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
      background-color: #6860ff;
      border-color: #6860ff;
   }
   :deep(.el-checkbox__inner:hover) {
      border-color: #626aef !important;
   }
   :deep(.el-checkbox__input.is-indeterminate .el-checkbox__inner) {
      background-color: #6860ff;
   }
   :deep(.el-checkbox__input.is-indeterminate .el-checkbox__inner) {
      border-color: #6860ff;
   }
   :deep(.el-button:hover) {
      background-color: #e2e4fd;
      color: #6860ff;
   }
   :deep(.el-input__wrapper:hover) {
      box-shadow: inset 0 0 0 1px #6860ff;
   }
</style>
